#include "ThresholdForm.h"

ThresholdForm::ThresholdForm(QWidget *parent, Qt::Orientation orientation, QwtSlider::ScalePos scalePos, QwtSlider::BGSTYLE bgStyle)
    : QDialog(parent)
{
    ui.setupUi(this);

    maxValue = 1.0;
    minValue = -1.0;
    stepValue = 0.1;
    scaleDrawn = true;
    usingTicks = true;

    ui.SliderHigh->setOrientation(orientation);
    ui.SliderHigh->setScalePosition(scalePos);
    ui.SliderHigh->setBgStyle(bgStyle);
    ui.SliderHigh->setRange(minValue,maxValue,stepValue);
    ui.SliderHigh->setTracking(false); //Only emit when mouse released
    ui.SliderLow->setOrientation(orientation);
    ui.SliderLow->setScalePosition(scalePos);
    ui.SliderLow->setBgStyle(bgStyle);
    ui.SliderLow->setRange(minValue,maxValue,stepValue);
    ui.SliderLow->setTracking(false); //Only emit when mouse released

    updateScales();

    createConnections();
}

ThresholdForm::~ThresholdForm()
{
    //dtor
}

void ThresholdForm::setMax(double max)
{
    double tmpMin = 0.0, interval = 0.1;

    maxValue = max;
    tmpMin = ui.SliderHigh->minValue();
    interval = ui.SliderHigh->step();

    ui.SliderHigh->setRange(tmpMin,max,interval);
    ui.SliderLow->setRange(tmpMin,max,interval);

    updateScales();
}

void ThresholdForm::setMin(double min)
{
    double tmpMax = 1.0, interval = 0.1;

    minValue = min;
    tmpMax = ui.SliderLow->maxValue();
    interval = ui.SliderLow->step();

    ui.SliderHigh->setRange(min,tmpMax,interval);
    ui.SliderLow->setRange(min,tmpMax,interval);

    updateScales();
}

void ThresholdForm::setStep(double interval)
{
    double tmpMax = 1.0, tmpMin = 0.0;

    stepValue = interval;
    tmpMax = ui.SliderHigh->maxValue();
    tmpMin = ui.SliderLow->minValue();

    ui.SliderHigh->setRange(tmpMin,tmpMax,interval);
    ui.SliderLow->setRange(tmpMin,tmpMax,interval);
    usingTicks = false;

    updateScales();
}

void ThresholdForm::setNoOfMajorTicks(int ticks)
{
    ui.SliderHigh->setScaleMaxMajor(ticks);
    ui.SliderLow->setScaleMaxMajor(ticks);
    usingTicks = true;
}

void ThresholdForm::setNoOfMinorTicks(int ticks)
{
    ui.SliderHigh->setScaleMaxMinor(ticks);
    ui.SliderLow->setScaleMaxMinor(ticks);
    usingTicks = true;
}

//slots
void ThresholdForm::updateUpperValue(double value)
{
    emit upperValueChanged(value);
}

void ThresholdForm::updateLowerValue(double value)
{
    emit lowerValueChanged(value);
}

void ThresholdForm::updateScales()
{
    if(!usingTicks)
    {
        ui.SliderHigh->setScale(minValue,maxValue,stepValue);
        ui.SliderLow->setScale(minValue,maxValue,stepValue);
    }
    else
    {
        ui.SliderHigh->setScale(minValue,maxValue);
        ui.SliderLow->setScale(minValue,maxValue);
    }
}

void ThresholdForm::createConnections()
{
    connect(ui.SliderHigh, SIGNAL(valueChanged(double)), this, SLOT(updateUpperValue(double)));
    connect(ui.SliderLow, SIGNAL(valueChanged(double)), this, SLOT(updateLowerValue(double)));
}
